package story_user_group

import (
	"context"
	"encoding/json"
	"story/internal/svc"
	"story/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetStoriesLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetStoriesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetStoriesLogic {
	return &GetStoriesLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetStoriesLogic) GetStories(req *types.GetStoriesReq) (resp *types.GetStoriesRsp, err error) {

	stories, err := l.svcCtx.StoryService.GetStories(l.ctx, req.PageSize, req.PageNum)
	if err != nil {
		return nil, err
	}

	marshal, err := json.Marshal(stories)
	if err != nil {
		return nil, err
	}

	resp = &types.GetStoriesRsp{
		Data: types.StoryData{},
	}
	err = json.Unmarshal(marshal, &resp.Data.Stories)
	if err != nil {
		return nil, err
	}

	return
}
